#ifndef __CRYPTO_H_
#define __CRYPTO_H_

#include <openssl/des.h>
#include <openssl/aes.h>

#define DES 1
#define AES 2

#define DES_ENCRYPT 1
#define DES_DECRYPT 0

#define AES_ENCRYPT 1
#define AES_DECRYPT 0

#define ECB 1
#define CFB 2
#define OFB 3
#define CBC 4

/* En bytes */
#define DES_BLOCK_SIZE 8
#define DES_KEY_SIZE 8

/* En bytes */
#define AES_BLOCK_SIZE 16
#define AES_KEY_SIZE 16

/* Prepares the parameters to encrypt/decrypt using DES
 * Receives:
 * -> mode: ECB, CFB, OFB or CBC
 * -> password: from which to extract key and ivec;
      if password is NULL uses key and ivec
 * -> key
 * -> ivec
 * -> ks: a pointer where to place the key schedule
 * -> iv: a pointer where to place the initialization vector
 * Returns 0 in case of error, 1 if not
*/
int initDes( int mode, char *password, char *key, char *ivec, DES_key_schedule *ks, DES_cblock *iv );


/* Encrypts/decrypts a block of 8 bytes using DES
 * Receives:
 * -> operation: DES_ENCRYPT or DES_DESCRYPT
 * -> mode: ECB, CFB, OFB or CBC.
 * -> ks: key schedule 
 * -> iv: initialization vector
 * -> block: a block of 8 bytes to encrypt/decrypt
 * Returns the encrypted/decrypted block 
*/
char * des( int operation, int mode, DES_key_schedule *ks, DES_cblock *iv, char *block );


/* Prepares the parameters to encrypt/decrypt using AES
 * Receives:
 * -> mode: ECB, CFB, OFB or CBC
 * -> password: from which to extract key and ivec;
      if password is NULL uses key and ivec
 * -> aesKey: a pointer where to place the AES key
 * -> iv: a pointer where to place the initialization vector
 * Returns 0 in case of error, 1 if not
*/
int initAes( int operation, int mode, char *password, char *key, char *ivec, AES_KEY *aesKey, unsigned char *iv );


/* Encrypts/decrypts a block of 8 bytes using AES
 * Receives:
 * -> operation: AES_ENCRYPT or AES_DESCRYPT
 * -> mode: ECB, CFB, OFB or CBC.
 * -> aesKey: AES key
 * -> iv: initialization vector
 * -> block: a block of 8 bytes to encrypt/decrypt
 * Returns the encrypted/decrypted block 
*/
char * aes( int operation, int mode, AES_KEY *aesKey, unsigned char *iv, char *block );

#endif
